{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Export Network Set as Generalized MDIF File"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Frequently a set of `Networks` is recorded while changing some other parameters; like temperature, voltage, current, etc. Once this set of data acquired, it is sometime useful to combine all the networks into a single Generalized MDIF file for use in CAD tools like AWR Microwave Office."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pathlib\n",
    "import tempfile\n",
    "import zipfile\n",
    "\n",
    "import numpy as np\n",
    "import requests\n",
    "\n",
    "import skrf as rf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Narda 3752 phase shifter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example, we are characterizing an old [narda phase shifter 3752](https://nardamiteq.com/docs/119-PHASESHIFTERS.PDF) at 1.5 GHz. \n",
    "\n",
    "\n",
    "![narda 3752 phase shifter](phase_shifter_measurements/Narda_3752.jpg)\n",
    "\n",
    "In order to deduce the phase shift that one can obtain at this specific frequency, we have measured scattering parameters in the 1-2 GHz band at 19 positions of the phase knob (from 0 to 180). These measurements are loaded into a [NetworkSets](../../tutorials/NetworkSet.ipynb) object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Array containing the 19 phase shift indicator values\n",
    "indicators_mes = np.linspace(0, 180, num=19)  # from 0 to 180 per 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ntw_set = rf.NetworkSet.from_zip('phase_shifter_measurements/phase_shifter_measurements.zip')\n",
    "print('ntw_set contains', len(ntw_set), 'networks')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The content of the NetworkSet can be exported into a MDIF file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ntw_set.write_mdif(\"phase_shifter.mdif\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that is possible to tune the parameters, values and types defined in the MDIF file by passing the optional parameters  `values` and `data_types`. Hence, making \"indicator\" an MDIF variable of type \"double\" and save the NetworkSet to \"phase_shifter.mdif\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "values = {\"indicator\": indicators_mes}\n",
    "data_types = {\"indicator\": \"double\"}\n",
    "ntw_set.write_mdif(\"phase_shifter.mdif\", values, data_types)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ADRF5720\n",
    "\n",
    "[ADRF5720](https://www.analog.com/en/products/adrf5720.html) is 6-bit 9 kHz to 40 GHz digital step attenuator and is measured over temperature which results in many Touchstone files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# download the zip archive of ADRF5720 Touchstone files\n",
    "url = \"https://www.analog.com/media/en/simulation-models/s-parameters/ADRF5720_Sparameters.zip\"\n",
    "try:\n",
    "    response = requests.get(url, timeout=10)\n",
    "    open(\"ADRF5720.zip\", \"wb\").write(response.content)\n",
    "    tmpdir = pathlib.Path(tempfile.mkdtemp())\n",
    "    zf = zipfile.ZipFile(\"ADRF5720.zip\")\n",
    "    zf.extractall(path = tmpdir)\n",
    "    zf.close()\n",
    "\n",
    "    # filter out one file (which contains '5720_noDC')\n",
    "    input_files = [file for file in tmpdir.rglob('*.s2p') if '5720_noDC' not in file.stem]\n",
    "    ns = rf.NetworkSet(rf.read_all(files=[str(file) for file in input_files]))\n",
    "    print('ns contains', len(ns), 'networks')\n",
    "\n",
    "    # extract the attenuation value from the filenames and store in list\n",
    "    attn = []\n",
    "    temp = []\n",
    "    for f in input_files:\n",
    "        _,_,_,_,_,a,t = f.stem.split('_')\n",
    "        t = t.replace('M','-').replace('C','')\n",
    "        attn.append(float(a))\n",
    "        temp.append(int(t))\n",
    "\n",
    "    # sort files\n",
    "    v = list(zip(attn,temp,input_files))\n",
    "    v.sort()\n",
    "    (attn,temp,input_files) = list(zip(*v))\n",
    "\n",
    "    values = {'ATTEN': attn, 'TEMP_C': temp }\n",
    "    datatypes = {'ATTEN': 'double', 'TEMP_C': 'double'}\n",
    "\n",
    "    # write to a generalized MDIF file\n",
    "    ns.write_mdif(\"ADRF5720.mdif\", values, datatypes)\n",
    "\n",
    "except requests.ReadTimeout:\n",
    "    print('Timeout... skipping this example')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, the parameterized MDIF files can be imported into AWR Microwave Office:\n",
    "\n",
    "![AWR](import_mdif_to_awr_mwo.png)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f9c1ad921755d07784f600fc7c2c11a8933ade33898d5981f5e318f268234d48"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
